<?php
/**
 * Created by PhpStorm.
 * User: allen
 * Date: 2019/3/8
 * Time: 12:48
 */

namespace app\index\model;

use app\admin\validate\Lottery;
use think\Model;

class ArticleLog extends Model
{
    protected $createTime = true;
    protected $pk='id';

    /**
     * 检测
     * @param int $aid
     * @param $mid
     * @param int $form_id
     * @param int $mc_id
     * @return bool
     */
    public function check($aid=0,$mid,$form_id=0,$mc_id=0){
        $res = $this
            ->where('mc_id','eq',$mc_id)
            ->where('form_id','eq',$form_id)
            ->where('mid','eq',$mid)
            ->where('aid','eq',$aid)
            ->count();
        return $res?true:false;
    }

    /**
     * 添加分享记录
     * @param $form_id
     * @param $mid
     * @param $aid
     * @param $mc_id
     * @param string $out
     * @return bool
     */
    public function addLog($form_id,$mid,$aid,$mc_id,&$out=''){
        $validate = new \app\index\validate\ArticleLog();
        $data = [
            'mid'=>$mid,
            'aid'=>$aid,
            'mc_id'=>$mc_id,
            'create_time'=>time(),
            'form_id'=>$form_id
        ];
        if(!$validate->check($data)){
            $out = $validate->getError();
            return false;
        }
        if(!$this->insert($data)){
            return false;
        }
        return true;
    }
}